<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Log;

class Installment extends Model
{
    const STATUS_PENDING = 'penging';
    const STATUS_REPAYING = 'repaying';
    const STATUS_FINISHED = 'finished';

    public static $statusMap = [
        self::STATUS_PENDING => '未执行',
        self::STATUS_REPAYING => '还款中',
        self::STATUS_FINISHED => '已完成',
    ];

    protected $fillable = ['no','total_amount','count','fee_rate','fine_rate','status'];

    protected static function boot()
    {
        parent::boot();
        //监听模型创建事件，在写入数据库之前
        self::creating(function ($model) {
            //如果模型字段为空
            if(! $model->no) {
                //调查findAvableNo生成分期流水号
                $model->no = static :: findAvailableNo();
                if(! $model->no){
                    return;
                }
            }
        });
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function items()
    {
        return $this->hasMany(InstallmentItem::class);
    }

    public static function findAvailableNo()
    {
        //分期流水号前缀
        $prefix = date('YmdHis');
        for ($i=0; $i<10; $i++){
            //随机生成6位的数字
            $no = $prefix.str_pad(random_int(0,999999),6,0,STR_PAD_LEFT);
            //判断是否存在
            if(! static::query()->where('no',$no)->exists()) {
                return $no;
            }
        }
        Log::warning('find installment no failed');

        return false;
    }

    public function refreshRefundStatus()
    {
        //设定一个全部退款成功的标志位
        $allSuccess = true;
        // 重新加载 items，保证与数据库中数据同步
        $this->load(['items']);
        //再次遍历所有还款计划
        foreach ($this->items as $item) {
            //如果该还款计划已经还款，但退款状态不是成功
            if($item->paid_at && $item->refund_status !== InstallmentItem::REFUND_STATUS_SUCCESS) {
                //则将标志位标记为false
                $allSuccess = false;
                break;
            }
        }
        //如果所有退款都成功，则将对应商品订单的退款状态改为退款成功
        if($allSuccess) {
            $this->order->update([
                'refund_status' => Order::REFUND_STATUS_SUCCESS
            ]);
        }
    }
}
